package ccmis.finance.capital.action;

import java.math.BigDecimal;
import java.util.List;
import javax.ejb.EJB;

import ccmis.finance.capital.bo.InterestRateBO;
import ccmis.finance.capital.entity.InterestRate;
import jfox.platform.infrastructure.SuperAction;
import org.jfox.framework.annotation.Service;
import org.jfox.mvc.ActionContext;
import org.jfox.mvc.Invocation;
import org.jfox.mvc.PageContext;
import org.jfox.mvc.annotation.ActionMethod;
import org.jfox.mvc.validate.LongValidation;

/**
 * @author <a href="mailto:ren.yingb@gmail.com">Ren Yingbo</a>
 */
@Service(id="interestrate")
public class InterestRateAction extends SuperAction{
	
	@EJB
	InterestRateBO interestRateBO;
    /**
     * 获取所有贷款合同
     * @param actionContext
     * @throws Exception
     */
	@ActionMethod(name = "listinterestrate",successView = "ccmis/finance/capital/interestrate_list.vhtml")
	public void listInterestRate(ActionContext actionContext) throws Exception{
	  List<InterestRate> interestRates = interestRateBO.getAllInterestRates();
	  PageContext pageContext = actionContext.getPageContext();
	  pageContext.setAttribute("interestRates", interestRates);
	}
	/**
	 * 新增贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "newview",successView = "ccmis/finance/capital/new_interestrate.vhtml")
	public void newInterestRateView(ActionContext actionContext) throws Exception{
		
	}
	/**
	 * 修改贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "editview",successView = "ccmis/finance/capital/edit_interestrate.vhtml",invocationClass = DelInterestRateInvocation.class)
	public void editInterestRateView(ActionContext actionContext) throws Exception{
		DelInterestRateInvocation invocation = (DelInterestRateInvocation)actionContext.getInvocation();
		InterestRate interestRate = interestRateBO.getInterestRateById(invocation.getId());
		actionContext.getPageContext().setAttribute("interestRate", interestRate);
	}
	/**
	 * 保存新建的贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "new",successView = "include/json_response.vhtml",errorView = "include/json_response.vhtml",invocationClass = CreateInterestRateInvocation.class)
	public synchronized void createInterestRate(ActionContext actionContext) throws Exception{
		CreateInterestRateInvocation invocation = (CreateInterestRateInvocation)actionContext.getInvocation();
		InterestRate interestRate = new InterestRate();
		interestRate.setContractID(invocation.getContractID());
		interestRate.setRNo(invocation.getRNo());
		interestRate.setInterestRate(invocation.getInterestRate());
		interestRate.setStartDate(jfox.platform.common.Utils.getMyLongDate(invocation.getStartDate()));
		interestRate.setEndDate(jfox.platform.common.Utils.getMyLongDate(invocation.getEndDate()));
		interestRate.setPriority(invocation.getPriority());
		interestRate.setVstatus(invocation.getVstatus());
		interestRateBO.newInterestRate(interestRate);
		actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, interestRate);
	}
	/**
	 * 保存修改的贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "edit",successView = "include/json_response.vhtml",errorView = "include/json_response.vhtml",invocationClass = EditInterestRateInvocation.class)
	public synchronized void editInterestRate(ActionContext actionContext) throws Exception{
		EditInterestRateInvocation invocation = (EditInterestRateInvocation)actionContext.getInvocation();
		InterestRate interestRate = interestRateBO.getInterestRateById(invocation.getId());
		interestRate.setContractID(invocation.getContractID());
		interestRate.setRNo(invocation.getRNo());
		interestRate.setInterestRate(invocation.getInterestRate());
		interestRate.setStartDate(jfox.platform.common.Utils.getMyLongDate(invocation.getStartDate()));
		interestRate.setEndDate(jfox.platform.common.Utils.getMyLongDate(invocation.getEndDate()));
		interestRate.setPriority(invocation.getPriority());
		interestRate.setVstatus(invocation.getVstatus());
		interestRateBO.updateInterestRate(interestRate);
		actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ, interestRate);
	}
	/**
	 * 删除贷款合同
	 * @param actionContext
	 * @throws Exception
	 */
	@ActionMethod(name = "delete",successView = "include/json_response.vhtml",errorView = "include/json_response.vhtml",invocationClass = DelInterestRateInvocation.class)
	public synchronized void deleteInterestRate(ActionContext actionContext) throws Exception{
		DelInterestRateInvocation invocation = (DelInterestRateInvocation)actionContext.getInvocation();
		actionContext.getPageContext().setAttribute(JSON_RESPONSE_OBJ,invocation.getId());
		interestRateBO.deleteInterestRate(invocation.getId());
	}
	
	public static class DelInterestRateInvocation extends Invocation{
	
		@LongValidation
		private long id;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}
		
	}
   
	public static class CreateInterestRateInvocation extends Invocation{
	
		private long contractID;
		
		private String rNo;
		
		private String startDate;
		
		private String endDate;
		
		private BigDecimal interestRate;
		
    	private int vstatus;
    	
    	private int priority;

		public long getContractID() {
			return contractID;
		}

		public void setContractID(long contractID) {
			this.contractID = contractID;
		}

		public String getRNo() {
			return rNo;
		}

		public void setRNo(String no) {
			rNo = no;
		}

		public String getStartDate() {
			return startDate;
		}

		public void setStartDate(String startDate) {
			this.startDate = startDate;
		}

		public String getEndDate() {
			return endDate;
		}

		public void setEndDate(String endDate) {
			this.endDate = endDate;
		}

		public BigDecimal getInterestRate() {
			return interestRate;
		}

		public void setInterestRate(BigDecimal interestRate) {
			this.interestRate = interestRate;
		}

		public int getVstatus() {
			return vstatus;
		}

		public void setVstatus(int vstatus) {
			this.vstatus = vstatus;
		}

		public int getPriority() {
			return priority;
		}

		public void setPriority(int priority) {
			this.priority = priority;
		}

		
	}
	
	public static class EditInterestRateInvocation extends CreateInterestRateInvocation{
		
		private long id;

		public long getId() {
			return id;
		}

		public void setId(long id) {
			this.id = id;
		}
	}
	
}
